﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using RiskMan.Config;
using RiskMan.DataClass.Models.NotBasModels;
using RiskMan.DataClass.Models.Plaza_II;
using RiskMan.Views.Dialogs;

namespace RiskMan.Views.Plaza_II
{
    /// <summary>
    /// Interaction logic for AddNewRobotPlazaII.xaml
    /// </summary>
    public partial class AddNewRobotPlazaII 
    {
        private RobotItemPlazaII Rpl;
        private DataClass.Data data = MainWindow.StaticLevel._dataProvider;
        public AddNewRobotPlazaII(RobotItemPlazaII rpl)
        {
            InitializeComponent();
            Rpl = rpl;



            //#region Счет
            //CmbxClientCode.ItemsSource = trades.Select(a => a.ClientCode).ToList();
            //for (int i = 0; i < CmbxClientCode.Items.Count; i++)
            //{
            //    if (CmbxClientCode.Items[i].ToString() == Rpl.ClientCode)
            //        CmbxClientCode.SelectedIndex = i;
            //}
            //#endregion

            #region Seccode
            CmbxInstrument.ItemsSource = data.GetSeccodeList();
            for (int i = 0; i < CmbxInstrument.Items.Count; i++)
            {
                if (CmbxInstrument.Items[i].ToString() == Rpl.Instrument)
                    CmbxInstrument.SelectedIndex = i;
            }
            #endregion

            TxbxQuantity.Text = Rpl.Quantity.ToString();
            TxbxNumTicksForSmallCross.Text = Rpl.NumTicksForSmallCross.ToString();
            TxbxNumTicksForBigCross.Text = Rpl.NumTicksForBigCross.ToString();

            #region Индикаторы

            TxbxSmallSlowForSignalLine.Text = Rpl.SmallFastForSignalLine.ToString();
            TxbxSmallFastForSignalLine.Text = Rpl.SmallSlowForSignalLine.ToString();
            TxbxSmallMacd.Text = Rpl.SmallMacd.ToString();
            TxbxNumTicksForInit.Text = Rpl.NumTicksForInit.ToString();

            TxbxBigFastForSignalLine.Text = Rpl.BigFastForSignalLine.ToString();
            TxbxBigSlowForSignalLine.Text = Rpl.BigSlowForSignalLine.ToString();
            TxbxBigMacd.Text = Rpl.BigMacd.ToString();

            TxbxSmallPivotHigh.Text = Rpl.SmallPivotHigh.ToString();
            TxbxSmallPivotLow.Text = Rpl.SmallPivotLow.ToString();

            TxbxBigPivotHigh.Text = Rpl.BigPivotHigh.ToString();
            TxbxBigPivotLow.Text = Rpl.BigPivotLow.ToString();

            #endregion

            //параметры на заявки на открытие
            cmbxTypeOrderOpen.ItemsSource = new List<string>() { Cfg.openOrderLimit, Cfg.openOrderMarket };
            cmbxTypeOrderOpen.SelectedIndex = 0;
            cmbxKillOrderOpen.ItemsSource = new List<string>() { Cfg.Unknown, Cfg.KillofTime, Cfg.KillOfDictance, Cfg.KillOfTimeOrDistance };
            cmbxKillOrderOpen.SelectedIndex = 0;
            if (Rpl.SettingOrderOpenClose == null)
                Rpl.SettingOrderOpenClose = new SettingOrderOpenClosePoz();
            if (Rpl.SettingOrderOpenClose.TypeOrderOpen != null)
                cmbxTypeOrderOpen.SelectedValue = Rpl.SettingOrderOpenClose.TypeOrderOpen;
            if (Rpl.SettingOrderOpenClose.KillOrderOpen != null)
                cmbxKillOrderOpen.SelectedValue = Rpl.SettingOrderOpenClose.KillOrderOpen;

            txbxOtstupForOpenOrder.Text = Rpl.SettingOrderOpenClose.OtstupForOpenOrder.ToString();
            txbxTimeWiatOpenPoz.Text = Rpl.SettingOrderOpenClose.TimeWiatOpenPoz.ToString();
            txbxSpredForKillOrderOpen.Text = Rpl.SettingOrderOpenClose.SpredForKillOrderOpen.ToString();


            //Параметры стоп-заявки
            cmbxTypeofPricing.ItemsSource = new List<string>() { Cfg.TypeCloseOrderAuto, Cfg.TypeCloseOrderManual };
            cmbxTypeStop.ItemsSource = new List<string>() { Cfg.LinkedOrder };
            cmbxTypeofPricing.SelectedIndex = 0;
            cmbxTypeStop.SelectedIndex = 0;
            if (Rpl.SettingOrderOpenClose.TypePricing != null)
                cmbxTypeofPricing.SelectedValue = Rpl.SettingOrderOpenClose.TypePricing;
            if (Rpl.SettingOrderOpenClose.TypeStop != null)
                cmbxTypeStop.SelectedValue = Rpl.SettingOrderOpenClose.TypeStop;

            txbxStopLimitOtstup.Text = Rpl.SettingOrderOpenClose.StopLimitOtstup.ToString();
            txbxStopLimitSpred.Text = Rpl.SettingOrderOpenClose.StopLimitSpred.ToString();
            txbxLimitOrderProfit.Text = Rpl.SettingOrderOpenClose.Profit.ToString();
        }

        #region Обработка получение текстобксом фокуса и потери его, замена запятой на точку

        private void TxbxLostFocus(object sender, RoutedEventArgs e)
        {
            if (((TextBox)sender).Text == "")
                ((TextBox)sender).Text = 0.ToString();

            char pointChar = ',';
            string str = (1.2).ToString();
            if (str.IndexOf('.') > 0) pointChar = '.';

            if (pointChar == ',')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace('.', pointChar);

            if (pointChar == '.')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace(',', pointChar);


        }

        private void TxbxGotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((TextBox)sender).Text != "")
                {
                    if (Convert.ToDouble(((TextBox)sender).Text) == 0)
                    {
                        ((TextBox)sender).Text = "";
                    }
                }
            }
            catch
            {
                ((TextBox)sender).Text = "";
            }
        }

        private void TxbxPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;

            if ((e.Text.IndexOf('.') == 0 || e.Text.IndexOf(',') == 0) && ((TextBox)sender).Text.IndexOf('.') == -1 && ((TextBox)sender).Text.IndexOf(',') == -1)
                e.Handled = false;
        }

        private void TxbxDitgitPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;
        }
        #endregion

        private void ButtonAccept_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //if (CmbxClientCode.SelectedIndex >= 0)
                //{
                //    Rpl.ClientCode = CmbxClientCode.SelectedValue.ToString();
                //    List<Trade> tradeList = MainWindow.StaticLevel._dataProvider.GetTradeList();
                //    if (tradeList != null)
                //        for (int i = 0; i < tradeList.Count; i++)
                //            if (tradeList[i].ClientCode == Rpl.ClientCode)
                //                Rpl.Account = tradeList[i].Account;
                //}
                //else
                //{
                //    new DialogOkCancel("Выберите код клиента(счет)!", "Ошибка добавления");
                //    return;
                //}


                //if (CmbxInstrument.SelectedIndex >= 0)
                //    if (CmbxInstrument.SelectedValue.ToString() != "Не выбран")
                //        Rpl.Instrument = CmbxInstrument.SelectedValue.ToString();
                //    else
                //    {
                //        new DialogOkCancel("Выберите торгуемый инструмент!", "Ошибка добавления");
                //        return;
                //    }
                //else
                //{
                //    new DialogOkCancel("Выберите торгуемый инструмент!", "Ошибка добавления");
                //    return;
                //}

                Rpl.GuidItem = Guid.NewGuid();

                Rpl.Quantity = Convert.ToInt32(TxbxQuantity.Text);
                Rpl.NumTicksForSmallCross = Convert.ToInt32(TxbxNumTicksForSmallCross.Text);
                Rpl.NumTicksForBigCross = Convert.ToInt32(TxbxNumTicksForBigCross.Text);

                #region Индикаторы

                Rpl.SmallFastForSignalLine = Convert.ToInt32(TxbxSmallSlowForSignalLine.Text);
                Rpl.SmallSlowForSignalLine = Convert.ToInt32(TxbxSmallFastForSignalLine.Text);
                Rpl.SmallMacd = Convert.ToInt32(TxbxSmallMacd.Text);
                Rpl.NumTicksForInit = Convert.ToInt32(TxbxNumTicksForInit.Text);

                Rpl.BigFastForSignalLine = Convert.ToInt32(TxbxBigFastForSignalLine.Text);
                Rpl.BigSlowForSignalLine = Convert.ToInt32(TxbxBigSlowForSignalLine.Text);
                Rpl.BigMacd = Convert.ToInt32(TxbxBigMacd.Text);


                Rpl.SmallPivotHigh = Convert.ToInt32(TxbxSmallPivotHigh.Text);
                Rpl.SmallPivotLow = Convert.ToInt32(TxbxSmallPivotLow.Text);

                Rpl.BigPivotHigh = Convert.ToInt32(TxbxBigPivotHigh.Text);
                Rpl.BigPivotLow = Convert.ToInt32(TxbxBigPivotLow.Text);

                #endregion

                Rpl.TradeList = new ObservableCollection<TradesOpen>();

                Rpl.SettingOrderOpenClose = new SettingOrderOpenClosePoz();

                Rpl.SettingOrderOpenClose.TypeOrderOpen = cmbxTypeOrderOpen.Text;
                Rpl.SettingOrderOpenClose.KillOrderOpen = cmbxKillOrderOpen.Text;
                Rpl.SettingOrderOpenClose.OtstupForOpenOrder = Convert.ToDouble(txbxOtstupForOpenOrder.Text);
                Rpl.SettingOrderOpenClose.TimeWiatOpenPoz = Convert.ToInt32(txbxTimeWiatOpenPoz.Text);
                Rpl.SettingOrderOpenClose.SpredForKillOrderOpen = Convert.ToDouble(txbxSpredForKillOrderOpen.Text);

                Rpl.SettingOrderOpenClose.TypePricing = cmbxTypeofPricing.Text;
                Rpl.SettingOrderOpenClose.TypeStop = cmbxTypeStop.Text;
                Rpl.SettingOrderOpenClose.StopLimitOtstup = Convert.ToDouble(txbxStopLimitOtstup.Text);
                Rpl.SettingOrderOpenClose.StopLimitSpred = Convert.ToDouble(txbxStopLimitSpred.Text);
                Rpl.SettingOrderOpenClose.Profit = Convert.ToDouble(txbxLimitOrderProfit.Text);

                DialogResult = true;
                Close();
            }
            catch
            { }
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
